<template>
  <el-dialog v-model="visible" title="设备充值" :close-on-click-modal="false" destroy-on-close>
    <el-form ref="dataFormRef" label-width="90px" :model="dataForm" :inline="true">
      <el-row>
        <el-col :span="9">
          <el-form-item label="设备数量" prop="number">
            <el-input v-model="dataForm.number" placeholder="" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="购买类型" prop="goodsId">
            <el-select
                v-model="dataForm.goodsId"
                placeholder="请选择"
                size="default"
                style="width: 200px"
                @change="goodsIdChange"
            >
              <el-option
                  v-for="item in goodsData"
                  :key="item.id"
                  :label="item.goodsName"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="支付金额" prop="data">
            <template #default>
              <span>{{ dataForm.data }}</span>
            </template>
          </el-form-item>
        </el-col>
        <div class="display">
          <el-form-item label="支付方式" v-if="dataForm.channelId === 'WX_NATIVE'" prop="recharge">
            <el-radio-group v-model="dataForm.channelId">
              <el-radio v-auth="`gps:renewal:recharge:wx`" :value="'WX_NATIVE'">微信</el-radio>
              <!--              <el-radio :value="2">支付宝</el-radio>-->
              <el-radio v-auth="`gps:renewal:recharge:channelId`" disabled :value="'SUPER_ADMIN'">支付宝</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="dataForm.channelId === 'WX_NATIVE' && show" label="请扫码支付" prop="recharge">
            <el-radio-group>
              <el-avatar shape="square" :size="100" :src="url" />
            </el-radio-group>
          </el-form-item>
        </div>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="pay">支付</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import QRCode from 'qrcode'
import { goodsDataApi, payApi, payOrderApi, setRechargeListApi } from '@/api/gps/recharge/recharge'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
const router = useRouter()
const emit = defineEmits(['refreshDataList'])
const url = ref()
const goodsData = ref()
const visible = ref(false)
const show = ref(false)
// const payType = ref('WX_NATIVE')
const data = reactive({
  imeis: [],
  returnUrl: import.meta.env.VITE_API_URL
})
const dataForm = reactive({
  goodsId: '',
  number: '',

  data: '',
  value: 1,
  channelId: 'WX_NATIVE'
})
const dataFormRef = ref()
const init = (imeis, type) => {
  dataForm.channelId = type
  dataForm.goodsId = ''
  dataForm.data = ''
  visible.value = true
  show.value = false
  getgoodsData()
  // setRechargeList(imeis)
  data.imeis = imeis
  dataForm.number = String(data.imeis.length)
}

function getgoodsData(){
  goodsDataApi({ pageNum: 1, pageSize: 100 }).then((res) => {
    goodsData.value = res.data.list.filter((res) => {
      if(res.id === 'plat_0002' || res.id === 'FXFT_YD_000089' ){
        return res
      }
    })
  })
}

function goodsIdChange() {
  setRechargeList()
}


function pay() {
  payApi({ imeis: data.imeis, goodsId: dataForm.goodsId }).then((ref) => {
    payOrderApi({ goodsOrderId: ref.data, channelId: dataForm.channelId }).then((res) => {
      if (res.data !== '') {
        let img = QRCode.toDataURL(res.data)
        img.then((t) => {
          show.value = true
          url.value = t
        })
        return
      }
      ElMessage.success({
        message: '充值成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

function setRechargeList() {
  // console.log(data.returnUrl)
  setRechargeListApi({ imeis: data.imeis, goodsId: dataForm.goodsId })
    .then((ref) => {
      dataForm.data = ref.data
    })
    .catch((res) => {
      visible.value = false
    })
}

defineExpose({
  init
})
</script>
<style>
.display {
  display: flex;
  flex-direction: column;
}
</style>
